﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Castle.ActiveRecord.Framework;
using Castle.ActiveRecord;
using System.Reflection;
using Castle.ActiveRecord.Framework.Config;
using Matriculex.Dominio;

namespace Matriculex.Testes
{
    /// <summary>
    /// Summary description for TesteActiveRecord
    /// </summary>
    [TestClass]
    public class TesteActiveRecord
    {
        public TesteActiveRecord()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext) {
            IConfigurationSource config = ActiveRecordSectionHandler.Instance;
            ActiveRecordStarter.Initialize(Assembly.Load("Matriculex.Dominio"), config);
            ActiveRecordStarter.CreateSchema();
        }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void TestDisciplina()
        {
            Disciplina disc = new Disciplina();
            disc.Codigo = "PCS2046";
            disc.Nome = "Lógica computacional";
            disc.Save();

            Disciplina[] disc2 = Disciplina.FindAllByProperty("Codigo", "PCS2046");
            Assert.IsTrue(disc2.Length > 0);
        }

        [TestMethod]
        public void TestAluno()
        {
            TestDisciplina();

            Aluno aluno = new Aluno();
            aluno.Email = "paul@macca";
            aluno.Endereco = "Liverpool";
            aluno.IsBolsista = false;
            aluno.NumeroPECE = "5434452";
            aluno.Senha = "asd";

            Disciplina disc = Disciplina.FindAllByProperty("Codigo", "PCS2046")[0];
            aluno.AdicionarDisciplina(disc);

            aluno.Save();

            Aluno aluno2 = Aluno.FindAll().Where(a => a.DisciplinasPreSelecionadas[0].Codigo == "PCS2046").First();
            Assert.IsTrue(aluno2.Email == "paul@macca");

            disc = Disciplina.FindAllByProperty("Codigo", "PCS2046")[0];
            Assert.IsTrue(disc.AlunosPreInscritos[0].Email == "paul@macca");
        }
    }
}
